{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "07e3d3e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba.analyse\n",
    "from jieba.analyse import extract_tags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ae8779e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 1.210 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TF-IDF：  编程语言 1.426971375275\n",
      "TF-IDF：  在校生 1.3403279777\n",
      "TF-IDF：  硕士 1.1087799663225\n",
      "TF-IDF：  计算机 0.85059805386\n",
      "TF-IDF：  数学 0.826042559475\n",
      "TF-IDF：  专业 0.7741456821725\n",
      "TF-IDF：  学校 0.71152323753\n",
      "TF-IDF：  重点 0.6565789565175\n"
     ]
    }
   ],
   "source": [
    "# 基于 TF-IDF 算法的关键词抽取\n",
    "# jieba.analyse.extract_tags(sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))\n",
    "#   sentence 为待提取的文本\n",
    "#   topK 为返回几个 TF/IDF 权重最大的关键词，默认值为 20\n",
    "#   withWeight 为是否一并返回关键词权重值，默认值为 False\n",
    "#   allowPOS 仅包括指定词性的词，默认值为空，即不筛选\n",
    "# jieba.analyse.TFIDF(idf_path=None) 新建 TFIDF 实例，idf_path 为 IDF 频率文件\n",
    "sentence = \"本科及以上学历，计算机、数学等相关专业重点学校在校生(硕士为佳)-- 至少掌握一门编程语言，包括SQL。熟悉Linux；\"\n",
    "keywords = extract_tags(sentence, topK=20, withWeight=True, allowPOS=('n'))\n",
    "for item in keywords:\n",
    "    print('TF-IDF： ', item[0], item[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "46dd2fbf",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'function' object has no attribute 'set_idf_path'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-6e011ae6e51b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 词表\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 关键词提取所使用逆向文件频率（IDF）文本语料库可以切换成自定义语料库的路径\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mextract_tags\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_idf_path\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'word_frequency.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mkeywords\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjieba\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0manalyse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextract_tags\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msentence\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtopK\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwithWeight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mallowPOS\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'n'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'nr'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ns'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mkeywords\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'function' object has no attribute 'set_idf_path'"
     ]
    }
   ],
   "source": [
    "# 词表\n",
    "# 关键词提取所使用逆向文件频率（IDF）文本语料库可以切换成自定义语料库的路径\n",
    "jieba.analyse.set_idf_path('word_frequency.txt')\n",
    "keywords = jieba.analyse.extract_tags(sentence, topK=20, withWeight=True, allowPOS=('n', 'nr', 'ns'))\n",
    "for item in keywords:\n",
    "    print('TF-IDF加载逆向文件频率： ', item[0], item[1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
